package com.service.impl;

import com.Result.CommentInformation;
import com.Result.CommentResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.TwitterComment;
import com.mapper.TwitterCommentMapper;
import com.mapper.UserMapper;
import com.service.TwitterCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TwitterCommentServiceImpl extends ServiceImpl<TwitterCommentMapper, TwitterComment> implements TwitterCommentService {
    @Autowired
    private TwitterCommentMapper twitterCommentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public CommentResult getByTwitterId(Integer twitterId) {
        QueryWrapper<TwitterComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("twitter_id", twitterId);
        queryWrapper.eq("thumbs_up", 1);
        List<TwitterComment> list = this.twitterCommentMapper.selectList(queryWrapper);
        List<String> userList = this.getThumbsUpUserList(list);
        List<Integer> userIdList = this.getThumbUpUserIdList(list);
        List<CommentInformation> commentInformationList = this.getCommentInformationList(twitterId);
        CommentResult commentResult = new CommentResult();
        commentResult.setCommentInformationList(commentInformationList);
        commentResult.setThumbsUpUserList(userList);
        commentResult.setThumbsUpUserIdList(userIdList);
        return commentResult;
    }

    private List<String> getThumbsUpUserList(List<TwitterComment> list) {
        return list.stream().map(twitterComment -> this.userMapper.selectById(twitterComment.getUserId()).getName()).collect(Collectors.toList());
    }

    private List<Integer> getThumbUpUserIdList(List<TwitterComment> list) {
        return list.stream().map(TwitterComment::getUserId).collect(Collectors.toList());
    }

    private List<CommentInformation> getCommentInformationList(Integer twitterId) {
        QueryWrapper<TwitterComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("twitter_id", twitterId);
        queryWrapper.eq("thumbs_up", 0);
        List<TwitterComment> list = this.twitterCommentMapper.selectList(queryWrapper);
        List<CommentInformation> commentInformationList = new ArrayList<>();
        for(TwitterComment twitterComment : list) {
            CommentInformation commentInformation = new CommentInformation();
            commentInformation.setCommentId(twitterComment.getId());
            commentInformation.setContent(twitterComment.getContent());
            commentInformation.setCommentUserName(this.userMapper.selectById(twitterComment.getUserId()).getName());
            commentInformationList.add(commentInformation);
        }
        return commentInformationList;
    }

    @Override
    public boolean commentForTwitter(Integer twitterId, Integer userId, String comment) {
        TwitterComment twitterComment = new TwitterComment();
        twitterComment.setTwitterId(twitterId);
        twitterComment.setUserId(userId);
        twitterComment.setContent(comment);
        twitterComment.setThumbsUp(0);
        return this.twitterCommentMapper.insert(twitterComment) > 0;
    }

    @Override
    public boolean thumbsUpForTwitter(Integer twitterId, Integer userId) {
        TwitterComment twitterComment = new TwitterComment();
        twitterComment.setThumbsUp(1);
        twitterComment.setUserId(userId);
        twitterComment.setTwitterId(twitterId);
        return this.twitterCommentMapper.insert(twitterComment) > 0;
    }

    @Override
    public boolean thumbsDownForTwitter(Integer twitterId, Integer userId) {
        QueryWrapper<TwitterComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("twitter_id", twitterId)
                .eq("user_id", userId)
                .eq("thumbs_up", 1);
        return this.twitterCommentMapper.delete(queryWrapper) > 0;
    }
}
